<?php
    
	include_once "include/constants.php";
	
    define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

    try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
         
		$users = array
					(
						'1' => array(),
						'2' => array(),
						'3' => array(),
						'4' => array(),
						'5' => array(),
						'6' => array(),
						'6' => array(),
						'7' => array(),
						'8' => array(),
						'9' => array(),
						'10' => array(),
						'11' => array(),
						'12' => array(),
						'13' => array(),
						'14' => array(),
						'15' => array(),
						'16' => array(),
						'17' => array(),
						'18' => array(),
						'19' => array(),
						'20' => array(),
						'21' => array(),
						'22' => array(),
						'23' => array(),
						'24' => array(),
						'25' => array(),
						'26' => array(),
						'27' => array(),
						'28' => array(),
						'29' => array(),
						'30' => array(),
					); 
		 
		 
		foreach  ($users as $usr=>$movieRates){
			for ($i = 1; $i <= 20; $i++) {
				$stmt = $conn->prepare('select m.movieid, m.title, m.imdbid
									from gg_top_movies m
									order by rand() limit 1;');
				$stmt->execute();
				$moviedata = $stmt->fetch();
				while (isset($movieRates[$moviedata['movieid']])){
					$stmt->execute();
					$moviedata = $stmt->fetch();
				}
				$moviedata['title'] = substr($moviedata['title'], 0, strlen($moviedata['title']) - 7);
				$movieRates[$moviedata['movieid']] = array
														("title"=> $moviedata['title'],
														"rating" =>  rand ( 1 , 10 ),
														"imdbid" => $moviedata["imdbid"],
														);
				$users[$usr] = $movieRates;
				
			}	
			
		}
		echo json_encode($users);
		
		
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
?>


